﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CitrixSystemVerifier
{
    class ContextValue
    {
        private char[] delimiters = new char[] { ',' };

        public ContextValue()
        {
            m_Value = null;
        }

        public ContextValue(string Value)
        {
            m_Value = Value;
        }

        public ContextValue(string context, string Value)
        {
            Context = context;
            m_Value = Value;
        }

        public ContextValue(List<string> Contexts, string Value)
        {
            m_Value = Value;
            m_Contexts = Contexts;
        }

        private string m_Value;
        public string Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }

        private List<string> m_Contexts = new List<string>();
        public string Context
        {
            get
            {
                string s = "";
                foreach (string ss in m_Contexts)
                {
                    if (!string.IsNullOrEmpty(s))
                        s += ",";
                    s += ss;
                }
                return s;
            }

            set 
            {
                string c = value.Replace('+', ',');

                if (c != null)
                {
                    string[] ParsedContext = null;
                    ParsedContext = c.Split(delimiters);

                    foreach (string s in ParsedContext)
                        m_Contexts.Add(s.Trim());
                }
            }
        }

        public bool IsValid ()
        {
            if (string.IsNullOrEmpty(m_Value))
                return false;
            if (m_Contexts.Count == 0)
                return false;
            return true;
        }

        public int HasContext (string[] ParsedContext)
        {
            if (m_Contexts.Count == 0)
                return 0;
            if (ParsedContext.Length == 0)
                return 0;
            int ContextMatches = 0;
            foreach (string c in m_Contexts)
            {
                foreach (string sub in ParsedContext)
                   if (c.Trim() == sub.Trim())
                        ContextMatches++;
            }

            return ContextMatches;
        }

        public bool Matches (string[] Context)
        {
            if ((Context == null) && (m_Contexts.Count == 0))
                return true;

            int nMatches = 0;
            for (int i = 0; i < Context.Length; i++)
            {
                string s = m_Contexts.Find(c => c == Context[i]);
                if (!string.IsNullOrEmpty(s))
                    nMatches++;
            }
            return (nMatches == Context.Length) && (nMatches == m_Contexts.Count);
        }
    }

    class ContextValues
    {
        private char[] delimiters = new char[] {','};

        public ContextValues()
        {
        }

        public bool IsEmpty ()
        {
            if (m_Values.Count == 0)
                return true;
            
            return false;
        }

        public List<ContextValue> FindBestMatch (string Context)
        {
            string[] ParsedContext = null;
            if (Context != null)
                ParsedContext = Context.Split(delimiters);

            List<ContextValue> BestContextValues = new List<ContextValue> ();
            int ContextValueMatches = 0;
            foreach (ContextValue cv in m_Values)
            {
                int Matches = cv.HasContext(ParsedContext);
                if (Matches > ContextValueMatches)
                {
                    ContextValueMatches = Matches;
                    BestContextValues.Clear();
                    BestContextValues.Add(cv);
                }
                else if (Matches == ContextValueMatches)
                {
                    BestContextValues.Add(cv);
                }
            }
            return BestContextValues;
        }

        public int FindExactIndex(string Context)
        {
            string[] ParsedContext = null;
            if (Context != null)
                ParsedContext = Context.Split(delimiters);

            for (int index = 0; index < m_Values.Count; index++)
            {
                int Matches = m_Values[index].HasContext(ParsedContext);
                if (Matches == ParsedContext.Length)
                    return index;
            }
            return -1;
        }

        public void Set(string Context, string Value)
        {
            Context = Context.Replace('+', ',');

            int index = FindExactIndex(Context);
            if (index == -1)
            {
                ContextValue cv = new ContextValue(Context, Value);
                m_Values.Add(cv);
            }
            else
            {
                m_Values[index].Value = Value;
            }
        }

        public void Set (ContextValue cv)
        {
            int index = FindExactIndex(cv.Context);
            if (index == -1)
            {
                m_Values.Add(cv);
            }
            else
            {
                m_Values[index].Value = cv.Value;
            }
        }

        public List<string> Get(string Context)
        {
            List<ContextValue> cvs = FindBestMatch(Context);
            if ((cvs != null) && (cvs.Count > 0))
            {
                List<string> Values = new List<string>();
                foreach (ContextValue cv in cvs)
                    Values.Add(cv.Value);
                return Values;
            }
            return null;
        }

        private List<ContextValue> m_Values = new List<ContextValue>();
    }
}
